var em_number, st, currentID, beginRegistraTime, beginRegistraTime, flag = true;
var path = $("#path").val();
function Mesload() {
	$('#table')
			.bootstrapTable(
					{
						method : "POST",
						url : "cp_index.json",
						striped : true,
						singleSelect : false,
						dataType : "json",
						pagination : true, // 分页
						pageSize : 10,
						pageNumber : 1,
						search : false, // 显示搜索框
						contentType : "application/x-www-form-urlencoded",
						queryParams : null,
						columns : [

								{
									checkbox : "true",
									field : 'id',
									align : 'center',
									valign : 'middle'
								},
								{
									title : "薪酬标准编码",
									field : 'em_number',
									align : 'center',
									valign : 'middle'
								},
								{
									title : '薪酬标准名称',
									field : 'position_name',
									align : 'center',
									valign : 'middle'
								},
								{
									title : '审核状态',
									field : 'st_state',
									align : 'center',
								/*
								 * formatter : function(value, row, index) { }
								 */

								},

								{
									title : '登记时间',
									field : 'em_registrationtime',
									align : 'center',
									valign : 'middle',
									formatter : function(value, row, index) {
										return changeDateFormat(value)
									}
								},
								{
									title : '操作',
									field : '',
									align : 'center',
									formatter : function(value, row) {

										var del = '<a href="#"  onclick="del(\''
												+ row.em_number + '\')">删除</a> '
										var edit = '<a href="#"  onclick="edit(\''
												+ row.id + '\')">修改</a> '
										var change = '<a href="Gocp_salary_view1.html?id='
											+ row.em_number + '">变更</a> '
										var audit = '<a href="#" onclick="audit(\''
												+ row.id + '\')">提交复核</a> '
										var detail = '<a href="Gocp_salary_view.html?id='
												+ row.em_number + '">明细</a> ';
										var detail1 = '<a href="Gocp_salary_view1.html?id='
												+ row.em_number + '">明细</a> ';
										if (row.st_state == "起草") {
											return edit + detail1+ del+change;
										} else if (row.st_state == "已驳回") {
											return audit + detail1+del+change;
										} else if (row.st_state == "审核通过") {
											return change + detail;
										} else if (row.st_state == "审核中") {
											return   detail;
										}

									}
								} ]
					});
	getData();
}
// 初始化状态下拉菜单
$.ajax({
	type : "POST",// 请求类型
	url : "getStatus.json",// 请求的url
	dataType : "json",// ajax接口（请求url）返回的数据类型
	success : function(data) {// data：返回数据（json对象）
		if (data != null) {
			var stid = $("#st_id").val();
			$("st").html("");// 通过标签选择器，得到select标签，适用于页面里只有一个select
			var options = "<option value=\"0\">请选择</option>";
			for (var i = 0; i < data.length; i++) {
				// alert(data[i].id);
				// alert(data[i].proName);
				if (stid != null && stid != undefined && data[i].id == stid) {
					options += "<option selected=\"selected\" value=\""
							+ data[i].st_id + "\" >" + data[i].st_state
							+ "</option>";
				} else {
					options += "<option value=\"" + data[i].st_id + "\" >"
							+ data[i].st_state + "</option>";
				}

			}
			$("select").html(options);
		}
	}
});

!function() {
	laydate.skin('molv');// 切换皮肤，请查看skins下面皮肤库
	laydate({
		elem : '#beginRegistraTime'
	});// 绑定元素
	laydate({
		elem : '#endRegistraTime'
	});// 绑定元素
}();
function changeDateFormat(cellval) {
	if (cellval != null) {
		var date = new Date(parseInt(cellval));
		var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1)
				: date.getMonth() + 1;
		var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date
				.getDate();

		var hh24 = date.getHours();
		var mi = date.getMinutes();
		var ss = date.getSeconds();

		return date.getFullYear() + "-" + month + "-" + currentDate + " "
				+ hh24 + ":" + mi + ":" + ss;
	}
}

function getData() {
	if (flag) {
		em_number = "";
		st = "";
		beginRegistraTime = "";
		endRegistraTime = ""
		em_number = $("#em_number").val();
		st = $("#st").val();
		beginRegistraTime = $("#beginRegistraTime").val();
		endRegistraTime = $("#endRegistraTime").val();
	}
	$.ajax({
		type : "GET",
		url : "cp_index.json?em_number=" + em_number + "&st=" + st
				+ "&beginRegistraTime=" + beginRegistraTime
				+ "&endRegistraTime=" + endRegistraTime,
		dataType : "json",
		success : function(result) {
			if (result != null) {
				var TableData = result;
				$('#table').bootstrapTable("load", TableData);
			}
		}
	})
}

function add() {
	openlayer()
	currentID = "";
}

function edit(id) {
	openlayer()
	currentID = id;
}

function del(id) {
	var Id = id;
     if(confirm("是否删除薪酬单"+Id)){
    		$.ajax({
    			url : 'delCp?em_number=' + Id,
    			type : 'GET',
    			dataType : 'json',
    			success : function(data) {
    				if (data.data) {
    					alert("删除成功！")
    					getData();
    				} else {
    					alert("删除失败，该薪酬单下还有未删除的人事表")
    				}
    			},
    			error : function(err) {
    			}
    		});
     }
}

function getCurrentID() {
	return currentID;
}

function openlayer(id) {
	layer.open({
		type : 2,
		title : '添加信息',
		shadeClose : true,
		shade : 0.5,
		skin : 'layui-layer-rim',
		// maxmin: true,
		closeBtn : 1,
		area : [ '98%', '90%' ],
		shadeClose : true,
		closeBtn : 1,
		content : 'mesage_tail.html'
	// iframe的url
	});

}